package cn.iocoder.yudao.module.ym.controller.admin.worktemp;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ym.controller.admin.worktemp.vo.*;
import cn.iocoder.yudao.module.ym.dal.dataobject.worktemp.WorkTempDO;
import cn.iocoder.yudao.module.ym.service.worktemp.WorkTempService;

@Tag(name = "管理后台 - 工单模板")
@RestController
@RequestMapping("/ym/work-temp")
@Validated
public class WorkTempController {

    @Resource
    private WorkTempService workTempService;

    @PostMapping("/create")
    @Operation(summary = "创建工单模板")
    public CommonResult<Long> createWorkTemp(@Valid @RequestBody WorkTempSaveReqVO createReqVO) {
        return success(workTempService.createWorkTemp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单模板")
    public CommonResult<Boolean> updateWorkTemp(@Valid @RequestBody WorkTempSaveReqVO updateReqVO) {
        workTempService.updateWorkTemp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单模板")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteWorkTemp(@RequestParam("id") Long id) {
        workTempService.deleteWorkTemp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<WorkTempRespVO> getWorkTemp(@RequestParam("id") Long id) {
        WorkTempDO workTemp = workTempService.getWorkTemp(id);
        return success(BeanUtils.toBean(workTemp, WorkTempRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工单模板")
    public CommonResult<List<WorkTempRespVO>> getWorkTempList(@Valid WorkTempPageReqVO pageReqVO) {
        List<WorkTempDO> pageResult = workTempService.getWorkTempList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkTempRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单模板分页")
    public CommonResult<PageResult<WorkTempRespVO>> getWorkTempPage(@Valid WorkTempPageReqVO pageReqVO) {
        PageResult<WorkTempDO> pageResult = workTempService.getWorkTempPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkTempRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单模板 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkTempExcel(@Valid WorkTempPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkTempDO> list = workTempService.getWorkTempPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工单模板.xls", "数据", WorkTempRespVO.class,
                        BeanUtils.toBean(list, WorkTempRespVO.class));
    }

}